﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYRElectric.WirePlace;

namespace HYRElectric.Equipment
{
	public partial class DragLineLayoutForm : System.Windows.Forms.Form
	{
		public DragLineLayoutForm(Document document)
		{
			this.InitializeComponent();
			this.doc = document;
		}

		private void DragLineLayoutForm_Load(object sender, EventArgs e)
		{
			this.InitForm();
			this.ReadFormDataFromXml();
		}

		private void InitForm()
		{
			//base.Icon = Resource.YArchitechIcon;
			this.txtSpace.MostValue = new MostValue(10.0, 100000.0);
			this.txtSpaceToStart.MostValue = new MostValue(0.0, 100000.0);
			this.txtAngle.MostValue = new MostValue(0.0, 360.0);
			this.cmbWireStyle.Items.Add("弧线");
			this.cmbWireStyle.Items.Add("倒角");
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\DragLine";
			xmlHelper.SelectNode(path);
			if (Convert.ToInt32(xmlHelper.GetAttributeEx("Align", "0")) == 0)
			{
				this.radioStart.Checked = true;
			}
			else
			{
				this.radioCenter.Checked = true;
			}
			this.UpdateSpaceToStartTextbox();
			string attributeEx = xmlHelper.GetAttributeEx("Space", "2000");
			this.txtSpace.Text = attributeEx;
			attributeEx = xmlHelper.GetAttributeEx("SpaceToStart", "1000");
			this.txtSpaceToStart.Text = attributeEx;
			bool @checked = Convert.ToBoolean(xmlHelper.GetAttributeEx("IsAutoAngle", "true"));
			this.checkAngle.Checked = @checked;
			this.UpdateAngleTextbox();
			attributeEx = xmlHelper.GetAttributeEx("Angle", "0");
			this.txtAngle.Text = attributeEx;
			@checked = Convert.ToBoolean(xmlHelper.GetAttributeEx("IsLink", "false"));
			this.checkLinkLine.Checked = @checked;
			this.UpdateWireControl();
			int selectedIndex = Convert.ToInt32(xmlHelper.GetAttributeEx("WireStyle", "1"));
			this.cmbWireStyle.SelectedIndex = selectedIndex;
			int x = Convert.ToInt32(xmlHelper.GetAttributeEx("X", "100"));
			int y = Convert.ToInt32(xmlHelper.GetAttributeEx("Y", "100"));
            base.Location = new System.Drawing.Point(x, y);
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\DragLine";
			xmlHelper.SelectNode(path);
			xmlHelper.SetAttribute("Align", this.AlignWay.ToString());
			xmlHelper.SetAttribute("Space", this.Space.ToString());
			xmlHelper.SetAttribute("SpaceToStart", this.SpaceToStart.ToString());
			xmlHelper.SetAttribute("IsAutoAngle", this.IsAutoAngle.ToString());
			xmlHelper.SetAttribute("Angle", this.Angle.ToString());
			xmlHelper.SetAttribute("IsLink", this.IsLink.ToString());
			xmlHelper.SetAttribute("WireStyle", this.WireStyle.ToString());
			xmlHelper.SetAttribute("X", base.Location.X.ToString());
			xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			xmlHelper.SaveXml("");
		}

		private void UpdateSpaceToStartTextbox()
		{
			this.labelSpaceToStart.Enabled = this.radioStart.Checked;
			this.txtSpaceToStart.Enabled = this.radioStart.Checked;
		}

		private void UpdateAngleTextbox()
		{
			this.labelAngle.Enabled = !this.checkAngle.Checked;
			this.txtAngle.Enabled = !this.checkAngle.Checked;
		}

		private void UpdateWireControl()
		{
			this.buttonWire.Enabled = this.checkLinkLine.Checked;
			this.cmbWireStyle.Enabled = this.checkLinkLine.Checked;
		}

		private void radioStart_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateSpaceToStartTextbox();
		}

		private void checkAngle_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateAngleTextbox();
		}

		private void checkLinkLine_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateWireControl();
		}

		private void buttonWire_Click(object sender, EventArgs e)
		{
			new NewWireParamForm(this.doc).ShowDialog();
		}

		private void DragLineLayoutForm_Deactivate(object sender, EventArgs e)
		{
			string text = this.txtSpace.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtSpace.Text = this.txtSpace.OldValue;
			}
			text = this.txtSpace.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtSpace.Text = "2000";
			}
			text = this.txtSpaceToStart.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtSpaceToStart.Text = this.txtSpaceToStart.OldValue;
			}
			text = this.txtSpaceToStart.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtSpaceToStart.Text = "1000";
			}
			text = this.txtAngle.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtAngle.Text = this.txtAngle.OldValue;
			}
			text = this.txtAngle.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtAngle.Text = "0";
			}
		}

		private void DragLineLayoutForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveFormDataToXml();
		}

		protected override void WndProc(ref Message msg)
		{
			if (msg.Msg == 274 && (int)msg.WParam == 61536)
			{
				base.Hide();
				return;
			}
			base.WndProc(ref msg);
		}

		public int AlignWay
		{
			get
			{
				if (!this.radioStart.Checked)
				{
					return 1;
				}
				return 0;
			}
		}

		public int Space
		{
			get
			{
				return Convert.ToInt32(this.txtSpace.Text.Trim());
			}
		}

		public int SpaceToStart
		{
			get
			{
				return Convert.ToInt32(this.txtSpaceToStart.Text.Trim());
			}
		}

		public bool IsAutoAngle
		{
			get
			{
				return this.checkAngle.Checked;
			}
		}

		public int Angle
		{
			get
			{
				return Convert.ToInt32(this.txtAngle.Text.Trim());
			}
		}

		public bool IsLink
		{
			get
			{
				return this.checkLinkLine.Checked;
			}
		}

		public int WireStyle
		{
			get
			{
				return this.cmbWireStyle.SelectedIndex;
			}
		}

		public void UpdateData()
		{
			try
			{
				int num = int.Parse(this.txtSpace.Text);
				if (num <= 0)
				{
					num = 2000;
				}
				this.txtSpace.Text = num.ToString();
			}
			catch
			{
				this.txtSpace.Text = "2000";
			}
			try
			{
				int num2 = int.Parse(this.txtAngle.Text);
				this.txtAngle.Text = num2.ToString();
			}
			catch
			{
				this.txtAngle.Text = "0";
			}
		}

		private const string XmlNode_Root = "DragLine";

		private const string XmlNode_Align = "Align";

		private const string XmlNode_Space = "Space";

		private const string XmlNode_SpaceToStart = "SpaceToStart";

		private const string XmlNode_IsAutoAngle = "IsAutoAngle";

		private const string XmlNode_Angle = "Angle";

		private const string XmlNode_IsLink = "IsLink";

		private const string XmlNode_WireStyle = "WireStyle";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private Document doc;
	}
}
